import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../providers/article_provider.dart';
import '../config/app_theme.dart';

/// 公司筛选组件
class CompanyFilter extends StatelessWidget {
  const CompanyFilter({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer<ArticleProvider>(
      builder: (context, provider, child) {
        final companyList = provider.companyList;
        final selectedCompanyId = provider.selectedCompanyId;

        return Container(
          height: 100,
          decoration: const BoxDecoration(), // 透明背景，无边框
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            padding: const EdgeInsets.symmetric(horizontal: 16),
            itemCount: companyList.length,
            itemBuilder: (context, index) {
              final company = companyList[index];
              return _buildCompanyItem(
                context,
                isSelected: selectedCompanyId == company.companyId,
                companyName: company.companyNameCn ?? company.companyCode,
                logoUrl: company.logoUrl,
                companyCode: company.companyCode,
                onTap: () {
                  provider.selectCompany(company.companyId);
                },
              );
            },
          ),
        );
      },
    );
  }

  Widget _buildCompanyItem(
    BuildContext context, {
    required bool isSelected,
    bool isAll = false,
    String? companyName,
    String? logoUrl,
    String? companyCode,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: 70,
        margin: const EdgeInsets.only(right: 16),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Logo 圆形
            Container(
              width: 50,
              height: 50,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: isAll ? AppTheme.accentColor : AppTheme.bgHover,
                border: Border.all(
                  color: isSelected ? AppTheme.accentColor : Colors.transparent,
                  width: 2,
                ),
                boxShadow: isSelected
                    ? [
                        BoxShadow(
                          color: AppTheme.accentColor.withOpacity(0.4),
                          blurRadius: 8,
                          spreadRadius: 0,
                        ),
                      ]
                    : null,
              ),
              child: isAll
                  ? const Icon(
                      Icons.apps,
                      color: Color(0xFF1A1A1A),
                      size: 28,
                    )
                  : logoUrl != null
                      ? ClipOval(
                          child: Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: CachedNetworkImage(
                              imageUrl: logoUrl,
                              fit: BoxFit.contain,
                              placeholder: (context, url) => const Center(
                                child: CircularProgressIndicator(strokeWidth: 2),
                              ),
                              errorWidget: (context, url, error) => Center(
                                child: Text(
                                  companyCode?.substring(0, 1).toUpperCase() ?? 'T',
                                  style: const TextStyle(
                                    color: AppTheme.textSecondary,
                                    fontSize: 18,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        )
                      : Center(
                          child: Text(
                            companyCode?.substring(0, 1).toUpperCase() ?? 'T',
                            style: const TextStyle(
                              color: AppTheme.textSecondary,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
            ),
            const SizedBox(height: 6),
            // 公司名称
            Text(
              companyName ?? '',
              style: TextStyle(
                color: isSelected ? AppTheme.accentColor : AppTheme.textSecondary,
                fontSize: 12,
                fontWeight: isSelected ? FontWeight.w500 : FontWeight.normal,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              textAlign: TextAlign.center,
            ),
          ],
        ),
      ),
    );
  }
}

